<template>
    <div  v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="正在发布...">
        <el-button link type="success" size="small" @click="statusClick()" :disabled="status === 1">发布</el-button>
    </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { watch } from '@vue/runtime-core'
import { ElNotification, ElMessage,ElMessageBox } from 'element-plus'
import axios from 'axios';
import { useStore } from 'vuex'
export default {
    props:['data'],
    setup(props){
        const store = new useStore()
        let subjectId = ref()
        let status = ref()
        subjectId.value = props.data.subjectId
        status.value = props.data.status

        watch(()=>props.data,(newVal)=>{
            subjectId.value = newVal.subjectId
            status.value = newVal.status

        },{
            deep:true
        })
        function statusClick() {
            ElMessageBox.confirm(
                '确认发布?',
                '确认',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                        let data = {
                            subject:{
                                subjectId:subjectId.value
                            },
                            subjectPageDTO:{
                                subjectTypeId: store.state.questionBank.subjectModel.subjectPagination.subjectTypeId,
                                accountTypeId: store.state.questionBank.subjectModel.subjectPagination.accountTypeId,
                                status: store.state.questionBank.subjectModel.subjectPagination.status,
                                pageNumber: store.state.questionBank.subjectModel.subjectPagination.pageNum,
                                pageSize: store.state.questionBank.subjectModel.subjectPagination.pageSize
                            }
                        }
                        fullscreenLoading.value = true
                        axios.put("/api/subject/statusOneSubject",JSON.stringify(data),{
                            headers: {
                                'Content-Type': 'application/json;charset=UTF-8'
                            }
                        }
                        ).then((response) => {
                            if (response.status == 200) {
                                if (response.data.code == 200) {
                                    store.commit("selectSubjectTableData", response.data.data.list);
                                    store.commit("selectSubjectTableTotal", response.data.data.total);
                                    ElNotification({
                                        title: '操作成功',
                                        message: '发布成功',
                                        type: 'success',
                                    })
                                } else {
                                    ElNotification({
                                        title: '操作失败',
                                        message: '发布失败',
                                        type: 'error',
                                    })
                                }
                            } else {
                                ElNotification({
                                    title: '操作异常',
                                    message: '发布异常',
                                    type: 'warring',
                                })
                            }
                        }).finally(() => {
                            fullscreenLoading.value = false
                        })

            }).catch(() => {
                ElMessage('操作取消')
            })
        }
        let fullscreenLoading = ref(false)
        return {
            subjectId,
            status,
            fullscreenLoading,
            statusClick
        }
    }
}
</script>

<style>

</style>